$(document).ready(
		function() {
			$("#inputTotalPrice").val(0);
			// 库存显示的数据绑定
			var stockListVue = new Vue({
				el : '#stockDiv',
				data : {
					items : null
				},
				methods : {
					chooseQuantity : function(stock, index) {
						// 打开选择退货数量的输入框
						openChooseStockQuantity(stock, index);
					}
				}
			});
			// 获取库存list
			getStockList();

			// 退货商品的list数据绑定
			var returnMaterialVue = new Vue({
				el : '#returnMaterialDiv',
				data : {
					items : null
				},
				methods : {
					deleteMaterial : function(returnMaterial, index) {
						// 打开选择退货数量的输入框
						deleteReturnMaterial(returnMaterial, index);
					}
				}
			});

			// 供应商list的数据绑定
			var supplierListVue = new Vue({
				el : '#supplierDiv',
				data : {
					options : null
				},
				beforeCreate : function() {
					var _self = this;
					$.get(SERVER_PRE_URL + "user/getAllSupplierAjax",
							function(data) {
								_self.options = data;
							});
				}
			});
			// getSupplierList();

			// 退货商品的list
			var returnMaterialList = new Array();
			// 库存list
			var stockList = new Array();

			// 获得供应商list
			function getSupplierList() {
				$.get(SERVER_PRE_URL + "supplier/getAllSupplierAjax", function(
						data) {
					supplierListVue.options = data;
				});
			}

			// 搜索库存按钮的点击事件
			$("#searchStockButton").on(
					"click",
					function() {
						var data = {
							"searchWords" : $("#inputSearchWords").val()
						};
						baseUtil.httpSend(
								"stock/getStockByMaterialIdOrMaterialNameAjax",
								data, showSearchResult);
					});

			// 提交退货申请的点击事件
			$("#submitButton").on(
					"click",
					function() {
						var data = {
							"supplierId" : $("#inputSupplier").val(),
							"returnTime" : $("#inputReturnTime").val(),
							"totalPrice" : $("#inputTotalPrice").val(),
							"remark" : $("#inputRemark").val(),
							"returnMaterialList" : JSON
									.stringify(returnMaterialList)
						};
						baseUtil.httpSend("returnApply/createReturnApplyListAjax",
								data, showSubmitResult);
					});

			// 显示提交退货申请的结果
			function showSubmitResult(result) {
				if (result == 1) {
					alert("创建成功");
				} else {
					alert("创建失败");
				}
			}

			// 更新搜索结果的显示
			function showSearchResult(data) {
				stockListVue.items = data;
				for ( var i in data) {
					stockList.push(data[i]);
				}
			}

			// 删除已选择的退货商品
			function deleteReturnMaterial(returnMaterial, index) {
				layer.confirm('确认删除:' + returnMaterial.materialName, {
					btn : [ '确认', '取消' ]
				// 按钮
				}, function() {
					layer.msg('删除成功!');
					returnMaterialList.splice(index, 1);
					returnMaterialVue.items = returnMaterialList;
					updateReturnMaterialTotalPrice();
				}, function() {

				});

			}

			// 打开选择退货数量的输入框
			function openChooseStockQuantity(stock, num) {

				var a = layer.prompt({
					title : '选择洋葱数量,库存：' + stock.quantity + ',单位：'
							+ stock.material.unit.unitName
				}, function(quantity, index) {
					if (quantity > stock.quantity) {
						layer.msg('库存不足！请重新输入');
					} else {
						// 从库存list中隐藏已选择的
						stockList.splice(num, 1);
						stockListVue.items = stockList;

						var returnMaterial = {
							"materialId" : stock.materialId,
							"materialName" : stock.material.materialName,
							"materialQuantity" : quantity,
							"materialPrice" : stock.material.materialPrice,
							"totalPrice" : quantity
									* stock.material.materialPrice,
							"unitName" : stock.material.unit.unitName
						};
						returnMaterialList.push(returnMaterial);
						returnMaterialVue.items = returnMaterialList;

						updateReturnMaterialTotalPrice();

						layer.close(index);
					}
				});

			}

			// 更新退货的 总价
			function updateReturnMaterialTotalPrice() {
				var totalPrice = 0;
				for ( var i in returnMaterialList) {
					totalPrice = totalPrice + returnMaterialList[i].totalPrice;
				}
				$("#inputTotalPrice").val(totalPrice);
			}

			// 获取库存的list
			function getStockList() {
				$.get(SERVER_PRE_URL + "stock/getAllStockAjax", function(data) {
					stockListVue.items = data;
					for ( var i in data) {
						stockList.push(data[i]);
					}
				});
			}

		});